/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-08-15 16:28:19
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-09-21 15:15:58
 * @FilePath: \cxl-admin\src\pages\company\trial\list\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import * as tableConfig from './config.json'
import { companyTrial, trialType } from '@/apis/company/trial'
import { reactive } from 'vue'

export default class CompanyTrialData extends BaseData implements BaseMethod {
    searchCompanyName = ''
    dialog = reactive({
        visible: false, //显示隐藏
        title: '编辑', //标题
        width: '752px', //弹窗宽度
        type: 'edit', //弹窗类型
        companyId: '',
        companyName: '',
        remark: ''
    })
    constructor() {
        super(tableConfig)
        this.getData()
    }

    getData = (arg?: any) => {
        const obj: trialType = {
            size: this.pageData.pagination.pageSize,
            current: this.pageData.pagination.pageNo
        }
        if (this.searchCompanyName) {
            obj.companyName = this.searchCompanyName
        }
        companyTrial(obj).then(res => {
            if (res.data.value === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.data.total

                res.data.data.records.forEach((item: any) => {
                    const totalMapVal = item.systemFreeTotalCount
                        .slice(1, -1)
                        .split(/\s*,\s*/)
                        .map((chunk: any) => chunk.split(':'))
                    const totalMap = new Map(totalMapVal)

                    const remainMapVal = item.systemFreeRemainMapCount
                        .slice(1, -1)
                        .split(/\s*,\s*/)
                        .map((chunk: any) => chunk.split(':'))
                    const remainMap = new Map(remainMapVal)
                    item.cx_systemFreeTotalCount = totalMap.get('1')
                    item.wb_systemFreeTotalCount = totalMap.get('2')
                    item.xny_systemFreeTotalCount = totalMap.get('3')

                    item.cx_systemFreeRemainMapCount = remainMap.get('1')
                    item.wb_systemFreeRemainMapCount = remainMap.get('2')
                    item.xny_systemFreeRemainMapCount = remainMap.get('3')
                    this.pageData.table.data.push(item)
                })
            }
        })
    }
    onSearch = (arg?: any) => {
        console.log(arg)
        this.searchCompanyName = arg.custom.companyName
        this.getData()
    }
    onReset = (arg?: any) => {
        this.searchCompanyName = ''
        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.getData()
    }
    onOperation = (arg?: any) => {
        console.log(arg)
        const { type, data } = arg
        if (type === 'trial') {
            this.dialog.visible = true
            this.dialog.companyId = data.companyId
            this.dialog.companyName = data.companyName
            this.dialog.remark = data.remarks
        }
    }
    onFreeTrialDialogHidden = () => {
        this.dialog.visible = false
        this.getData()
    }
}
